/*
 * TrackFaces.cpp
 *
 *  Created on: Jun 1, 2009
 *      Author: andrei
 */

#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include "capture.h"
#include "gesture_rec.h"
#include "facedet.h"
#include "camshift_wrapper.h"


//// Constants
const char * DISPLAY_WINDOW = "DisplayWindow";
//#define OPENCV_ROOT  "C:/Program Files/OpenCV/1.0"


//// Global variables
IplImage  * pVideoFrameCopy = 0;


//// Function definitions
int initAll();
void exitProgram(int code);
void captureVideoFrame();


int TrackFaces()
{
	printf("Looking for a hand\n");
	CvRect * pFaceRect = 0;
	if( !initAll() ) exitProgram(-1);

	// Capture and display video frames until a face
	// is detected
	while( 1 )
	{
		// Look for a face in the next video frame
		captureVideoFrame();
		pFaceRect = detectFace(pVideoFrameCopy);

		// Show the display image
		cvShowImage( DISPLAY_WINDOW, pVideoFrameCopy );
		if( (char)27==cvWaitKey(10) ) exitProgram(0);

		// exit loop when a face is detected
		if(pFaceRect) {
			printf("hand was detected \n");
			break;
		}
	}

	// initialize tracking
	printf("starting tracking\n");
	startTracking(pVideoFrameCopy, pFaceRect);

	// Track the detected face using CamShift
	while( 1 )
	{
		CvBox2D faceBox;
		printf("loop\n");
		// get the next video frame
		captureVideoFrame();

		// track the face in the new video frame
		faceBox = track(pVideoFrameCopy);

		// Changment : retangle au lieu dune ellipse
		cvRectangle(pVideoFrameCopy,
				cvPoint((faceBox.center.x - faceBox.size.width),(faceBox.center.y - faceBox.size.width)),
				cvPoint((faceBox.center.x + faceBox.size.width),(faceBox.center.y + faceBox.size.width)),
				CV_RGB(255,0,0), 1, 8, 0 );
		cvShowImage( DISPLAY_WINDOW, pVideoFrameCopy );
		if( (char)27==cvWaitKey(10) ) break;
	}

	exitProgram(0);
	return 0;
}




int initAll()
{
	if( !initCapture() ) {
		return 0;
	}
	if( !initFaceDet("trainout_test2.xml"))	{
		return 0;
	}

	// Startup message tells user how to begin and how to exit
	printf( "\n********************************************\n"
	        "To exit, click inside the video display,\n"
	        "then press the ESC key\n\n"
			"Press <ENTER> to begin"
			"\n********************************************\n" );
	fgetc(stdin);

	// Create the display window
	cvNamedWindow( DISPLAY_WINDOW, 1 );
	//cvNamedWindow( "NB", 1);
	//cvNamedWindow( "cont", 1);
	// Initialize tracker
	captureVideoFrame();
	if( !createTracker(pVideoFrameCopy) ) {
		return 0;
	}

	// Set Camshift parameters
	setVmin(60);
	setSmin(50);

	return 1;
}


//////////////////////////////////
// exitProgram()
//
void exitProgram(int code)
{
	// Release resources allocated in this file
	cvDestroyWindow( DISPLAY_WINDOW );
	cvReleaseImage( &pVideoFrameCopy );

	// Release resources allocated in other project files
	closeCapture();
	closeFaceDet();
	//*****************
	//closeGestureRec();
	releaseTracker();

	exit(code);
}


//////////////////////////////////
// captureVideoFrame()
//
void captureVideoFrame()
{
	// Capture the next frame
	IplImage  * pVideoFrame = nextVideoFrame();
	if( !pVideoFrame ) exitProgram(-1);

	// Copy it to the display image, inverting it if needed
	if( !pVideoFrameCopy )
		pVideoFrameCopy = cvCreateImage( cvGetSize(pVideoFrame), 8, 3 );
	cvCopy( pVideoFrame, pVideoFrameCopy, 0 );
	pVideoFrameCopy->origin = pVideoFrame->origin;

	if( 1 == pVideoFrameCopy->origin ) // 1 means the image is inverted
	{
		cvFlip( pVideoFrameCopy, 0, 0 );
		pVideoFrameCopy->origin = 0;
	}
}

